//
//  YDEmoticonManager.swift
//  weibo
//
//  Created by 孙亚东 on 16/7/3.
//  Copyright © 2016年 Sunyadong. All rights reserved.
//

import UIKit

class YDEmoticonManager: NSObject {
    
    static var recentArr = [YDEmoticonModels]()
    static var defaultArr = YDEmoticonManager.getImageModel("com.sina.default")
    static var emojiArr = YDEmoticonManager.getImageModel("com.apple.emoji")
    static var LxhArr = YDEmoticonManager.getImageModel("com.sina.lxh")
   
    //获取数组
   class func getArray ()-> [[[YDEmoticonModels]]] {
    
        return [subModels(recentArr),subModels(defaultArr),subModels(emojiArr),subModels(LxhArr)]
    
    }
    
    //创建一个方法，获取图片
    class func getImageModel (path: String) -> [YDEmoticonModels] {
        
        //创建一个临时数组
        var tempArr: [YDEmoticonModels] = [YDEmoticonModels]()
    
        let bundlePath = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)
        
        //拼接字符串
        let packAgePath = (bundlePath as? NSString)?.stringByAppendingPathComponent(path)
        
        
        let filePath = (packAgePath as? NSString)?.stringByAppendingPathComponent("info.plist")
        
        //根据plist文件获取数组
        var dict = NSMutableDictionary(contentsOfFile: filePath!)
        
        
        let dicArray = dict!["emoticons"] as? [[String: String]]
        
        //遍历数组，进行字典转模
        for dic in dicArray! {
    
            //进行字典做模型
            let model = YDEmoticonModels(dic: dic)
            
            model.path = packAgePath
            
            tempArr.append(model)
  
        }
        
        return tempArr
    
    }
    
    //实现字典分割
 class  func subModels (modelsArr: [YDEmoticonModels]) -> [[YDEmoticonModels]] {
    
        let pageCount = (modelsArr.count - 1) / 20 + 1
        
        var tempArr = [[YDEmoticonModels]]()
        
        
        //进行循环分割
        for i in 0..<pageCount {
        
            var lenth = 20
            
            if i == pageCount - 1 {
            
                lenth = modelsArr.count - i * 20
            
            
            }
            //将数组按照范围进行分割
            let Arr: [YDEmoticonModels] = (modelsArr as NSArray).subarrayWithRange(NSMakeRange(i*20, lenth)) as! [YDEmoticonModels]

            
          //将该数组添加到一个临时数组中
            
            tempArr.append(Arr)
        
        }
 
        return tempArr
   
    }
    
    
    
    
    

}
